package edu.ccut.saturn.studio.dialog.createcomponentdialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnCreatComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.util.ConversionUtil;

public class SaturnCreateComponentDialog extends TitleAreaDialog {

	private Text idText;
	private Text nameText, keyText, paraKVText, fullKeyText, descriptionText;

	private Combo paraTypeCombo;

	private Label nameLabel, keyValueLabel, paraKVLabel, paraTypeLabel,
			labelFullKey, descriptionLabel;

	private List<SaturnParameter> list;

	private SaturnCreatComponentNode saturnCreatComponentNode;

	public SaturnCreateComponentDialog(Shell parentShell,
			SaturnCreatComponentNode saturnCreatComponentNode) {
		super(parentShell);
		this.saturnCreatComponentNode = saturnCreatComponentNode;
		this.list = saturnCreatComponentNode.getParameters();
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages
				.getString("SaturnCreateComponentDialog.createparameter")); //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);

		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		final GridData gd_container = new GridData(SWT.FILL, SWT.FILL, true,
				true);
		container.setLayoutData(gd_container);

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText(Messages.getString("SaturnCreateComponentDialog.24")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_idText.verticalIndent = 10;
		idText.setLayoutData(gd_idText);

		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 10;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel
				.setText(Messages.getString("SaturnCreateComponentDialog.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_nameText.verticalIndent = 10;
		nameText.setLayoutData(gd_nameText);

		keyValueLabel = new Label(container, SWT.NONE);
		final GridData gd_keyValueLabel = new GridData();
		gd_keyValueLabel.verticalIndent = 10;
		keyValueLabel.setLayoutData(gd_keyValueLabel);
		keyValueLabel.setText(Messages
				.getString("SaturnCreateComponentDialog.expression")); //$NON-NLS-1$

		keyText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_keyText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_keyText.verticalIndent = 5;
		keyText.setLayoutData(gd_keyText);

		paraKVLabel = new Label(container, SWT.NONE);
		final GridData gd_paraKVLabel = new GridData();
		gd_paraKVLabel.verticalIndent = 10;
		paraKVLabel.setLayoutData(gd_paraKVLabel);
		paraKVLabel.setText(Messages
				.getString("SaturnCreateComponentDialog.birthparameter")); //$NON-NLS-1$

		paraKVText = new Text(container, SWT.BORDER);
		paraKVText.setTextLimit(100);
		final GridData gd_paraKVText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_paraKVText.verticalIndent = 10;
		paraKVText.setLayoutData(gd_paraKVText);

		paraTypeLabel = new Label(container, SWT.NONE);
		final GridData gd_paraTypeLabel = new GridData();
		gd_paraTypeLabel.verticalIndent = 10;
		paraTypeLabel.setLayoutData(gd_paraTypeLabel);
		paraTypeLabel.setText(Messages
				.getString("SaturnCreateComponentDialog.parametertype")); //$NON-NLS-1$

		paraTypeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_paraTypeText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		gd_paraTypeText.verticalIndent = 5;
		paraTypeCombo.setLayoutData(gd_paraTypeText);
		paraTypeCombo.setItems(SelectionCandidate.classtypes);
		paraTypeCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (Messages.getString("SaturnCreateComponentDialog.23").equals(paraTypeCombo //$NON-NLS-1$
						.getText())) {
					fullKeyText.setEnabled(true);
					labelFullKey.setEnabled(true);
				} else {
					fullKeyText.setText(Messages.getString("SaturnCreateComponentDialog.14")); //$NON-NLS-1$
					fullKeyText.setEnabled(false);
					labelFullKey.setEnabled(false);
				}
			}
		});

		final Label fullKeyLabel = new Label(container, SWT.NONE);
		final GridData gd_fullKeyLabel = new GridData();
		gd_fullKeyLabel.verticalIndent = 10;
		fullKeyLabel.setLayoutData(gd_fullKeyLabel);
		fullKeyLabel.setText(Messages.getString("SaturnCreateComponentDialog.FullKey")); //$NON-NLS-1$

		fullKeyText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_fullKeyText = new GridData(SWT.FILL, SWT.FILL, true,
				false);
		gd_fullKeyText.verticalIndent = 5;
		fullKeyText.setLayoutData(gd_fullKeyText);
		fullKeyText.setEnabled(false);

		labelFullKey = new Label(container, SWT.NONE);
		labelFullKey.setImage(Activator.getImage(Images.AMPLIFY_PLUS));

		labelFullKey.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				// FilterDialogForAttributeValueAssignmentNode dialog1 = new
				// FilterDialogForAttributeValueAssignmentNode(
				// Display.getDefault().getShells()[0],
				// saturnCreatComponentNode);
				//
				// dialog1.setTitle(Messages
				// .getString("SaturnCreateComponentDialog.17")); //$NON-NLS-1$
				// dialog1.setElements(ConversionUtil
				// .conversion(saturnCreatComponentNode.getMapfromProject()
				// .keySet()));
				//
				// if (dialog1.open() == Window.OK) {
				// fullKeyText.setText((String) dialog1.getFirstResult());
				// }

				OneCompontentSearchDialog searchDialog = new OneCompontentSearchDialog(
						Display.getDefault().getShells()[0],
						saturnCreatComponentNode);
				searchDialog.setTitle(Messages
						.getString("SaturnCreateComponentDialog.17")); //$NON-NLS-1$

				if (searchDialog.open() == Window.OK) {
					fullKeyText.setText(((SearchResult) searchDialog
							.getFirstResult()).getKey());
				}

			}
		});

		labelFullKey.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				labelFullKey.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				labelFullKey.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		final GridData gd_labelFullKey = new GridData();
		gd_labelFullKey.verticalIndent = 5;
		labelFullKey.setLayoutData(gd_labelFullKey);

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		gd_descriptionLabel.verticalIndent = 10;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages
				.getString("SaturnCreateComponentDialog.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.FILL,
				true, false, 2, 1);
		gd_descriptionText.heightHint = 50;
		gd_descriptionText.verticalIndent = 5;
		descriptionText.setLayoutData(gd_descriptionText);

		nameText.setText(saturnCreatComponentNode.getName().trim());
		nameText
				.setSelection(saturnCreatComponentNode.getName().trim().length());
		idText.setText(saturnCreatComponentNode.getId().trim());
		keyText.setText(saturnCreatComponentNode.getKey().trim());
		descriptionText
				.setText(saturnCreatComponentNode.getDescription().trim());

		paraKVText.setText(list.get(2).getValueKey());
		paraTypeCombo.setText(ConversionUtil.quotationRemove(list.get(0)
				.getValueKey()));

		if (Messages.getString("SaturnCreateComponentDialog.SaturnData") //$NON-NLS-1$
				.equals(paraTypeCombo.getText())) {
			fullKeyText.setEnabled(true);
			labelFullKey.setEnabled(true);
			fullKeyText.setText(saturnCreatComponentNode.getFullKey().trim());
		} else {
			fullKeyText.setText("");
			fullKeyText.setEnabled(false);
			labelFullKey.setEnabled(false);
		}

		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if ("".equals(nameText.getText().trim())) {
					setErrorMessage(Messages
							.getString("SaturnCreateComponentDialog.namelimit")); //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});

		return area;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			if (!saturnCreatComponentNode.getParameters().get(2).getValueKey()
					.equals(paraKVText.getText())
					|| !saturnCreatComponentNode.getParameters().get(0)
							.getValueKey().equals(paraTypeCombo.getText())) {
				saturnCreatComponentNode.modifyShape();
			}
			for (int i = 0; i < list.size(); i++) {
				quotationAdd(list.get(i), i);
			}

			list.get(2).setValueKey(paraKVText.getText().trim());
			list
					.get(2)
					.setClassType(
							"".equals(paraTypeCombo.getText().trim()) ? "java.lang.String"
									: paraTypeCombo.getText().trim());
			saturnCreatComponentNode.setDescription(descriptionText.getText()
					.trim());
			saturnCreatComponentNode.setFullKey(fullKeyText.getText().trim()
					.length() == 0 ? "" : "'" + fullKeyText.getText().trim()
					+ "'");
			saturnCreatComponentNode.setName(nameText.getText().trim());

		}
		super.buttonPressed(buttonId);
	}

	protected Point getInitialSize() {
		return new Point(500, 380);
	}

	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}

	// 添加单引号
	private void quotationAdd(SaturnParameter paramter, int i) {
		switch (i) {
		case 0:
			paramter.setValueKey("".equals(paraTypeCombo.getText().trim()) ? Messages.getString("SaturnCreateComponentDialog.25") //$NON-NLS-2$
					: "'" + paraTypeCombo.getText().trim() + "'");
			break;
		case 1:
			paramter
					.setValueKey((("".equals(fullKeyText.getText().trim()) || !("edu.ccut.saturn.component.SaturnData"
							.equals(paraTypeCombo.getText().trim())))) ? ""
							: "'" + fullKeyText.getText().trim() + "'");
			break;
		}
	}
}
